$(document).ready(function (){
require(['accordion','inputdrop','tree'],function (accordion ,inputdrop,tree){

var el_accordion = $("#accordion");

var list_current_type;

var list_current_data_name;

var current_selected_node_id;

var current_selected_node_name;

var paramObj = new Object();

view_init();

event_init();

$("#accordion_icon").find("div").eq(0).click();

function view_init()
{
	$("#accordion_icon").find("div").click(function (){
		list_current_data_name = $(this).attr("data-name");
		$("#accordion_icon").find("div").removeClass("active");
		$(this).addClass("active");
		tree_get();
	});
	$("#accordion").slimscroll()
}

function event_init()
{
	$("#candidate_asset_btn").click(function(){
		candidate_asset_tem_init();
	});
	$("#delete_btn").click(function(){
		selected_asset_list_batch_delete();
	});
}

function tree_get(currentNode)
{
	var self = this;
	var left_nav_tree_url;
	paramObj = {};
	if(list_current_data_name == "assetType")
	{
		left_nav_tree_url = "siem/logcollection/getAssertTree";
	}
	else
	{
		left_nav_tree_url = "siem/logcollection/getAgentTree";
	}
	um_ajax_get({
		url : left_nav_tree_url,
		isLoad : true,
		successCallBack : function (data1){
			var data;
			if(list_current_data_name == "assetType")
			{
				data = data1;
				data.push({id:"root" ,parent:"-1" ,label:"资产类型"});
				current_selected_node_id = "105105";
			}
			else
			{
				data = data1;
				data.push({id:"root" ,parent:"-1" ,label:"代理服务器"});
				current_selected_node_id = "100002";
			}
			if(currentNode)
			{
				current_selected_node_id = currentNode;
			}
			tree.render($("#accordion") ,{
				pId:"parentID",
				label:"label",
				id:"id",
				zNodes : data,
				expandNode : "root",
				selectNode : current_selected_node_id,
				zTreeOnClick : function (event, treeId, treeNode){
					current_selected_node_id = treeNode.id;
					current_selected_node_name = treeNode.label;
					if(list_current_data_name == "assetType")
					{
						paramObj.edTypeId = current_selected_node_id;
						if(treeNode.pId == "root")
						{
							g_dialog.operateAlert(null ,"请选择"+ treeNode.label+"下的资产类型。" ,"error");
							return false;
						}
					}
					else
					{
						paramObj.agentId = current_selected_node_id;
						// if(treeNode.pId == "root")
						// {
						// 	g_dialog.operateAlert($("[id=accordion]") ,"请选择"+ treeNode.label+"下的代理服务器。" ,"error");
						// 	return false;
						// }
					}
					if(treeNode.id == "root")
					{
						return false;
					}
					
					selected_asset_list_get({
						paramObj:paramObj,
						isLoad:true,
						maskObj:"body"
					});
				}
			});
			var treeObj = $("#accordion").data("tree").getSelectedNodes()[0];
			if (!treeObj)
				return
			current_selected_node_id = treeObj.id;
			current_selected_node_name = treeObj.label;
			if(list_current_data_name == "assetType")
			{
				paramObj.edTypeId = current_selected_node_id;
			}
			else
			{
				paramObj.agentId = current_selected_node_id;;
			}
			selected_asset_list_get({paramObj : paramObj ,isLoad : true ,maskObj : "body"});
		}
	});
}

function selected_asset_list_get(opt)
{
	var list_url;
	var header =  [	
		 		  {text:'',name:"t",width:3,hideSearch:"hide"},
				  {text:'资产名称',name:"edName",width:25,align:"left"},
				  {text:'资产IP',name:"ip",width:24,align:"left" ,searchRender:function (el){
						index_render_div(el ,{type:"ip"});
				  }},
				  {text:'编码方式',name:"codeType",align:"left",width:24,render:function (text){
				  	var msg;
				  	(text == "AUTO") && (msg = "自动检测");
				  	(text == "GBK") && (msg = "GBK");
				  	(text == "UTF-8") && (msg = "UTF-8");
				  	return msg;
				  },searchRender:function (el){
				  	var data = [
									{text:"----" ,id:"-1"},
			  						{text:"自动检测" ,id:"AUTO"},
			  						{text:"GBK" ,id:"GBK"},
			  						{text:"UTF-8" ,id:"UTF-8"}
						  		];
					g_formel.select_render(el ,{
						data : data,
						name : "codeType"
					});
				  }}
				 ];;
	if(list_current_data_name == "assetType")
	{
		list_url = "siem/logcollection/queryByAssetList";
		header.push({text:'代理服务器',name:"agentName",align:"left",width:24,searchRender : function (el){
				   	    secEl = $('<div class="inputdrop" id="nodeIds"></div>');
				   	    g_formel.code_list_render({
					   	   	key : "agentConfigList",
							agentEl : secEl
						});
				   	   el.append(secEl);
				    }}); 
	}
	else
	{
		list_url = "siem/logcollection/queryByAgentList";
		header.push( {text:'资产类型',name:"edTypeName",align:"left",width:24,searchRender : function (el){
				   	    assetTypeEl = $('<div class="inputdrop" id="edTypeIds" no-select="first-child"></div>');
						g_formel.code_list_render({
					   	   	key : "deviceAssetTypeSiemTree",
							deviceTypeEl : assetTypeEl
						});
					   	el.append(assetTypeEl);
					    }});
	}
	g_grid.render($("#table_div1"),{
		 header:header,
		 oper:[
				  {icon:"rh-icon rh-edit" ,text:"修改" ,aclick:selected_asset_list_update},
				  {icon:"rh-icon rh-delete" ,text:"删除" ,aclick:selected_asset_list_delete}
			   ],
		 operWidth:"90px",
		 url:list_url,
		 paramObj : opt.paramObj,
		 queryBefore:function (paramObj){
			paramObj.assetname = paramObj.edName;
			paramObj.ip6 = paramObj.ipv6Str
			if (list_current_data_name == "assetType")
			{
				paramObj.edTypeId = current_selected_node_id;
			}
			else
			{
				paramObj.agentId = current_selected_node_id;
			}
		},
		 maskObj:opt.maskObj,
		 isLoad:opt.isLoad,
		 dbIndex:1,
		 hasBorder : false,
		 autoHeight : true,
		 showCount : true
	});
}

function selected_asset_list_update(rowData)
{
	$.ajax({
		type: "GET",
		url: "module/monitor_config/log_occur_source_config_tpl.html",
		success :function(data)
		{
			g_dialog.dialog($(data).find("[id=selected_asset_edit_template]"),{
				width:"500px",
				init:init,
				title:"已选资产信息修改",
				saveclick:save_click
			});
		}
	});

	function init(el)
	{
		g_validate.init(el);
		var obj = new Object();
		if (list_current_data_name == "assetType") 
		{
			el.find("[id=agent_type]").remove();
			obj.treeType = "queryAssertNodeTree";
			obj.edId = rowData.edId;
			obj.agentId = rowData.agentId;
		}
		else
		{
			el.find("[id=asset_type]").remove();
			obj.treeType = "queryAgentNodeTree";
			obj.edTypeId = rowData.edTypeId;
			obj.edId = rowData.edId;
		}
		um_ajax_get({
			url:"siem/logcollection/queryDetail",
			paramObj:obj,
			isLoad:true,
			maskObj:"body",
			successCallBack:function(data){
				if (list_current_data_name == "assetType") 
				{
					el.umDataBind("render",data.detailStore);
					el.find("select").trigger("change");
				}
				else
				{
					g_formel.code_list_render({
				   	   	key : "deviceAssetTypeSiemTree",
						deviceTypeEl : el.find('[id=edTypeId]'),
						deviceTypeVal: data.detailStore.edTypeId
					});
					el.umDataBind("render",data.detailStore);
					el.find("select").trigger("change");
				}
			}
		});
	}
	function save_click(el,saveObj)
	{
		if(!g_validate.validate(el))
		{
			return false;
		}
		var obj = new Object();
		obj.edId = rowData.edId;
		obj.codeType = saveObj.codeType;
		obj.agentId = rowData.agentId;
		if (list_current_data_name == "assetType") 
		{
			obj.treeType = "queryAssertNodeTree";
		}
		else
		{
			obj.treeType = "queryAgentNodeTree";
			obj.globaledTypeId = rowData.edTypeId;
			obj.edTypeId = saveObj.edTypeId;
		}
		um_ajax_post({
			url:"siem/logcollection/doUpdate",
			paramObj:obj,
			isLoad:true,
			maskObj:"body",
			successCallBack:function(data){
				g_dialog.operateAlert();
				g_dialog.hide(el);
				selected_asset_list_get({paramObj : paramObj ,isLoad : true ,maskObj : "body"});
			}
		});
	}
}

function selected_asset_list_delete(rowData)
{
	var assetStoreList = [];
	if (list_current_data_name == "assetType") 
	{
		assetStoreList.push({edId:rowData.edId,
						 agentId:rowData.agentId,
						 edTypeId:current_selected_node_id});
	}
	else
	{
		assetStoreList.push({edId:rowData.edId,
						 agentId:current_selected_node_id,
						 edTypeId:rowData.edTypeId});
	}
	g_dialog.operateConfirm("确认删除选中的记录么?" ,{
		saveclick : function()
		{
			um_ajax_post({
				url : "siem/logcollection/delRow",
				paramObj : {assetStoreList:assetStoreList},
				successCallBack : function(data){
					g_dialog.operateAlert(null ,"操作成功！");
					tree_get(current_selected_node_id);
				},
				failCallBack : function (){
					g_grid.refresh($("#table_div1"));
					tree_get(current_selected_node_id);
				}
			});
		}
	});
}

function selected_asset_list_batch_delete()
{
	var dataArray = g_grid.getData($("[id=table_div1]") ,{chk : true});
	if(dataArray.length == 0){
		g_dialog.operateAlert(null ,index_select_one_at_least_msg ,"error");
		return false;
	}
	var assetStoreList = [];
	for (var i = 0; i < dataArray.length; i++) {
		if (list_current_data_name == "assetType") 
		{
			assetStoreList.push({edId:dataArray[i].edId,
							 agentId:dataArray[i].agentId,
							 edTypeId:current_selected_node_id});
		}
		else
		{
			assetStoreList.push({edId:dataArray[i].edId,
							 agentId:current_selected_node_id,
							 edTypeId:dataArray[i].edTypeId});
		}
	}

	g_dialog.operateConfirm("确认删除选中的记录么?" ,{
		saveclick : function()
		{
			um_ajax_post({
				url : "siem/logcollection/delRow",
				paramObj : {assetStoreList:assetStoreList},
				successCallBack : function(data){
					g_dialog.operateAlert(null ,"操作成功！");
					tree_get(current_selected_node_id);	
				}
			});
		}
	});
}

function candidate_asset_tem_init()
{
	$.ajax({
		type: "GET",
		url: "module/monitor_config/log_occur_source_config_tpl.html",
		success :function(data)
		{
			g_dialog.dialog($(data).find("[id=candidate_asset_template]"),{
				width:"800px",
				init:init,
				title:"待选资产",
				saveclick : save_click,
				autoHeight:"autoHeight"
			});
		}
	});

	function init(el)
	{
		g_validate.init(el);
		var currentName = current_selected_node_name.split("(")[0];
		var header = [
					{text:'',name:"t",width:3,hideSearch:"hide"},
					{text:"资产名称",name:"edName",align:"left",width:37},
					{text:"资产IP",name:"ip",align:"left",width:30,searchRender:function (el){
						index_render_div(el ,{type:"ip"});
					}},
					{text:"管控域",name:"securityDomainName",align:"left",width:30,searchRender : function (el){
				   	   secEl = $('<div class="inputdrop" id="sdomas"></div>');
				   	   g_formel.sec_biz_render({
				   	   		secEl : secEl
				   	   });
				   	   el.append(secEl);
				    }}
					];
		var obj = new Object();
		if (list_current_data_name == "assetType") 
		{
			el.find("[id=asset_type_div]").show();
			el.find("[data-id=edName]").text(currentName);
			el.find("[id=agent_div]").hide();
			el.find("[id=asset_div]").hide();
			header = [
					{text:'',name:"t",width:3,hideSearch:"hide"},
					{text:"资产名称",name:"edName",align:"left",width:25},
					{text:"资产IP",name:"ip",align:"left",width:24,searchRender:function (el){
						index_render_div(el ,{type:"ip"});
					}},
					{text:"管控域",name:"securityDomainName",align:"left",width:24,searchRender : function (el){
				   	   secEl = $('<div class="inputdrop" id="sdomas"></div>');
				   	   g_formel.sec_biz_render({
				   	   		secEl : secEl
				   	   });
				   	   el.append(secEl);
				    }},
				    {text:"代理服务器",name:"agentName",align:"left",width:24,searchRender : function (el){
				   	    secEl = $('<div class="inputdrop" id="nodeIds"></div>');
				   	    g_formel.code_list_render({
					   	   	key : "agentConfigList",
							agentEl : secEl
						});
				   	   el.append(secEl);
				    }}];
			obj.edTypeId = paramObj.edTypeId;
		}
		else
		{
			el.find("[id=asset_type_div]").hide();
			el.find("[id=agent_div]").show();
			el.find("[data-id=agentName]").text(currentName);
			el.find("[id=asset_div]").show();
			g_formel.code_list_render({
		   	   	key : "deviceAssetTypeSiemTree",
				deviceTypeEl : el.find('[id=edTypeId]')
			});
			obj.nodeId = paramObj.agentId;
		}
		g_grid.render(el.find("[id=table_div]"),{
			header:header,
			url:"siem/logcollection/queryAsserts",
			paramObj:obj,
			queryBefore:function (paramObj){
				paramObj.assetname = paramObj.edName;
				paramObj.ip6 = paramObj.ipv6Str
				if (list_current_data_name == "assetType") 
				{
					paramObj.edTypeId = current_selected_node_id;
				}
				else
				{
					paramObj.nodeId = current_selected_node_id;
				}
			},
			isLoad:true,
			maskObj:"body"
		});
	}

	function save_click(el,saveObj)
	{
		
		var dataArray = g_grid.getData(el.find("[id=table_div]") ,{chk : true});
		if(dataArray.length == 0){
			g_dialog.operateAlert(null ,index_select_one_at_least_msg ,"error");
			return false;
		} 
		var assetStoreList = [];
		for (var i = 0; i < dataArray.length; i++) {
			if (list_current_data_name == "assetType") 
			{
				assetStoreList.push({edId:dataArray[i].edId,
								 agentId:dataArray[i].agentId,
								 edTypeId:current_selected_node_id,
								 codeType:saveObj.codeType});
			}
			else
			{
				assetStoreList.push({edId:dataArray[i].edId,
								 agentId:current_selected_node_id,
								 agentName:current_selected_node_name,
								 edTypeId:saveObj.edTypeId,
								 codeType:saveObj.codeType});
			}
		}
		um_ajax_post({
			url : "siem/logcollection/addAssetForChange",
			paramObj : {assetStoreList:assetStoreList},
			maskObj:"body",
			isLoad:true,
			successCallBack : function(data){
				g_dialog.operateAlert();
				g_dialog.hide(el);
				tree_get(current_selected_node_id);
			}
		});
	}
}


});
});